#define _CRT_SECURE_NO_WARNINGS 1
#include <string>
#include <iostream>
using namespace std;
 struct ListNode {
  int val;
  struct ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
 };
class Solution {
public:
    string solve(string s, string t) {
        int i = s.size() - 1, j = t.size() - 1;
        int carry = 0;
        string result;
        while (i >= 0 && j >= 0) {
            int sum = s[i] - '0' + t[j] - '0' + carry;
            carry = sum / 10;
            cout << sum % 10 << " " << carry << endl;
            result.push_back(sum % 10 + '0');
            --i;
            --j;
        }
        while (i >= 0) {
            int sum = s[i] - '0' + carry;
            carry = sum / 10;
            cout << sum % 10 << " " << carry << endl;
            result.push_back(sum % 10 + '0');
            --i;
        }
        while (j >= 0) {
            int sum = t[j] - '0' + carry;
            carry = sum / 10;
            cout << sum % 10 << " " << carry << endl;
            result.push_back(sum % 10 + '0');
            --j;
        }
        if (carry != 0) result.push_back(carry + '0');
        reverse(result.begin(), result.end());
        return result;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        string s;
        while (head1)
        {
            s.push_back(head1->val + '0');
            head1 = head1->next;
        }
        string t;
        while (head2)
        {
            t.push_back(head2->val + '0');
            head2 = head2->next;
        }
        // cout<<s<<endl;
        // cout<<t<<endl;
        string result = solve(s, t);
        ListNode* ret = nullptr;
        ListNode* prev = ret;
        for (int i = 0;i < result.size();i++)
        {
            ListNode* n = new ListNode(result[i] - '0');
            if (i == 0) ret = n;
            else prev->next = n;
            prev = n;
        }
        return ret;
    }
};